package dtd.PHS.VND_ExchangeRate.data;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import dtd.PHS.VND_ExchangeRate.utils.Helpers;

public class SJCItem {
	private static final String SEP = "/";

	public static final long UPDATE_PERIOD = 5*60*1000; //5 minutes

	public static final String SJC = "SJC";

	

	private int selling;
	private int buying;
	private long createdTime;

	/**
	 * - This field is parse from the website, 
	 * - It is NOT the time the SJCItem created on client app
	 */
	private Date updateTime;



	public SJCItem(int buying, int selling) {
		this.selling = selling;
		this.buying = buying;
		this.createdTime = System.currentTimeMillis();
	}

	public SJCItem(String goldPrice) {
		ArrayList<String> words = Helpers.split(goldPrice, SEP);
		this.selling = Integer.parseInt(words.get(0));
		this.buying = Integer.parseInt(words.get(1));
		long time = Long.parseLong(words.get(2));
		this.updateTime = new Date(time);
		this.createdTime = Long.parseLong(words.get(3));
	}

	@Override
	public String toString() {
		long time = updateTime.getTime();
		return ""+selling+SEP+buying+SEP+time+SEP+createdTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public int getSelling() {
		return selling;
	}

	public void setSelling(int selling) {
		this.selling = selling;
	}

	public int getBuying() {
		return buying;
	}

	public void setBuying(int buying) {
		this.buying = buying;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public boolean isExpired() {
		long currentTimeMillis = System.currentTimeMillis();
		if ( currentTimeMillis - this.createdTime > UPDATE_PERIOD) {
			return true;
		}
		return false;
	}

	public String getUpdateTimeString() {
		SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa dd/MM/yyy");
		return sdf.format(updateTime);
	}


}
